var app = angular.module('ZJPC');
app.controller('CarFileCtrl', ['$scope','$state','$rootScope','$stateParams','DataService',CarFileCtrl]);

function CarFileCtrl($scope,$state,$rootScope,$stateParams,DataService) {

	$scope.search_entry = ""

	$scope.checkkey = function(e){
		var keycode = window.event?e.keyCode:e.which;
        if(keycode==13&&$scope.search_entry!=''){
            $scope.searchIt()
       	}
	}

	$scope.searchIt = function(){
		var re2 = /(^[\u4E00-\u9FA5]{1}[A-Z0-9]{6}$)|(^[A-Z]{2}[A-Z0-9]{2}[A-Z0-9\u4E00-\u9FA5]{1}[A-Z0-9]{4}$)|(^[\u4E00-\u9FA5]{1}[A-Z0-9]{5}[挂学警军港澳]{1}$)|(^[A-Z]{2}[0-9]{5}$)|(^(08|38){1}[A-Z0-9]{4}[A-Z0-9挂学警军港澳]{1}$)/
		var dataGroup = {};
		//分辨是车牌还是查询人
		if(re2.test($scope.search_entry)==true){
			DataService.getCar($scope.search_entry,function(data){
					console.log(data)
				//查不到值
				if(data==null||data.length==0){
					$scope.showNoResult = true;
					$scope.search_entry1 = $scope.search_entry;
				//查到了值
				}else{
					var url = $state.href('app.file.carfile.basic',{data:angular.toJson(data[0]),name:data[0].cph});
	  				window.open(url,'_blank');
				}
			})
		}else if($scope.search_entry.length<10){
			//代表是人
			DataService.getPerson($scope.search_entry,function(data){
			//人的情况下，有可能出现多个值，因此先判断data的个数
				//查不到值
				if(data==null||data.length==0){
					$scope.search_entry1 = $scope.search_entry;
					$scope.showNoResult = true;
				}else if(data.jid!=undefined){
					//一个值，直接进入下一步
					var url = $state.href('app.file.perfile.basic',{data:angular.toJson(data),jid:data.jid,xm:data.xm});
			  		window.open(url,'_blank');
				}else if(data.length==1){
					//查询姓名情况的单个值，直接进入下一步；
					var url = $state.href('app.file.perfile.basic',{data:angular.toJson(data[0]),jid:data[0].jid,xm:data[0].xm});
			  		window.open(url,'_blank');
				}else{
					//查询到多个值的情况，此时将显示一个界面，进行下一步判断
					$scope.searchList = data;
					$scope.showSearchList = true;
				}
			})
		
		}else{
			//以后加入mac地址等查询
		}
	}

	$scope.$watch('search_entry',function(newValue,oldValue){
		if(newValue!=oldValue||newValue.length==0){
			$scope.searchList = null;
			$scope.showSearchList = false;
			$scope.showNoResult = false;
		}
	})

	$scope.clickSearchList = function(data){
		var url = $state.href('app.file.perfile.basic',{data:angular.toJson(data),jid:data.jid,xm:data.xm});
		window.open(url,'_blank');
	}

		/*时间控件校验相关设置*/
	$scope.endDateBeforeRender = endDateBeforeRender
	$scope.endDateOnSetTime = endDateOnSetTime
	$scope.startDateBeforeRender = startDateBeforeRender
	$scope.startDateOnSetTime = startDateOnSetTime

	function startDateOnSetTime () {
	  $scope.$broadcast('start-date-changed');
	}

	function endDateOnSetTime () {
	  $scope.$broadcast('end-date-changed');
	}

	function startDateBeforeRender ($dates) {
	  if ($scope.endtime) {
	    $scope.endtime = new Date($scope.endtime).setHours(23,59,59)
	    var activeDate = moment($scope.endtime);

	    $dates.filter(function (date) {
	      return date.localDateValue() >= activeDate.valueOf()
	    }).forEach(function (date) {
	      date.selectable = false;
	    })
	  }
	}

	function endDateBeforeRender ($view, $dates) {
	  if ($scope.starttime) {
	    var activeDate = moment($scope.starttime).subtract(1, $view).add(1, 'minute');

	    $dates.filter(function (date) {
	      return date.localDateValue() <= activeDate.valueOf()
	    }).forEach(function (date) {
	      date.selectable = false;
	    })
	  }
	}
	
}